import time

import ADBHelper
import ImageProc

REFRESH_TIME = 5  # s


# 只点枪脚本，没枪随机
# REFRESH_TIME = 6  # s
#
#
# class Img:
#     screen = './cache/screenCap.png'
#     gun = './img/Fight-gun.png'


# def play_game(play_num=1):
#     device = ADBHelper.getDevicesList()[0]
#     print(f'当前连接的设备是: {device}')
#
#     while 1:
#
#         ADBHelper.touch(device, Point.start_game)
#
#         time.sleep(5)
#         # 刷新屏幕
#         ADBHelper.screenCapture(device, Img.screen)
#
#         point = ImageProc.locate(Img.screen, Img.gun)
#         if point:
#             ADBHelper.touch(device, point)
#         else:
#             ADBHelper.touch(device, Point. random_skill)
#
#         ADBHelper.touch(device, Point.jijia)
#         ADBHelper.touch(device, Point.exit_)


# 支持技能脚本
class Img:
  screen = './cache/screenCap.png'

  class Fight:
    start_game = './img/Fight-startGame.jpg'
    back = './img/Fight-back.png'
    page = (527, 2649, '战斗',)
    game_start = (630, 2312, '开始游戏',)
    game_continue_fight = (843, 2338, '继续战斗',)
    game_back_home = (843, 2338, '战斗结束-返回战斗开始页面',)
    game_advertisement_play = (0, 0, '战斗结束-广告-播放',)
    game_advertisement_close = (0, 0, '战斗结束-广告-关闭',)

    class Fighting:
      choose_skil_one_out_of_three = (621, 1810, '选择三选一技能',)
      release_mecha = (1111, 2023, '释放机甲',)
      release_pioneer_skills = (1120, 2298, '释放先锋技能',)

    class Skill:
      gun_子弹命中怪物后爆炸 = './img/Fight-skill-gun-baozha.png'
      gun_每次射击连发数加1 = './img/Fight-skill-gun-lianfashuliang.png'
      gun_子弹弹道数量加1 = './img/Fight-skill-gun-zidanshuliang.png'
      gun_将子弹变为火焰子弹 = './img/Fight_skill_gun_huoyanzidan.png'
      gun = './img/Fight-gun.png'

      fire_燃烧油桶 = './img/Fight-burnOil.png'
      car_车 = './img/Fight-car.png'
      boom_温压弹 = './img/Fight-boom.png'
      wind_旋风加农 = './img/Fight-wind.png'
      air_boom_空投 = './img/Fight-airBoom.png'
      light_射线 = './img/Fight-light.png'
      guided_laser_激光 = './img/Fight-guidedLaser.png'
      air_knife_风刃 = './img/Fight-airKnife.png'
      ice_bullet_干冰弹 = './img/Fight-iceBullet.png'
      drop_down = './img/Fight-dropDown.png'  # 精英
      diancicichuan_电磁 = './img/Fight-diancicichuan.png'
      bingbaofashengqi_冰爆 = './img/Fight-bingbaofashengqi.png'
      yueqiandianzi_电子 = './img/Fight-yueqiandianzi.png'
      wurenjichongji_无人机 = './img/Fight-wurenjichongji.png'
      dianjizhu_电极柱 = './img/Fight-dianjizhu.png'

      start_game = './img/Fight-startGame.jpg'
      back = './img/Fight-back.png'

      # blood = './img/Fight-blood.png'
      skill_first = [
        start_game, back,
        gun,
        # gun_子弹命中怪物后爆炸, gun_每次射击连发数加1, gun_子弹弹道数量加1, gun_将子弹变为火焰子弹,
        # fire_燃烧油桶, dianjizhu_电极柱, wind_旋风加农, car_车, light_射线, boom_温压弹,
        # wurenjichongji_无人机,
        # air_boom_空投, guided_laser_激光, air_knife_风刃, ice_bullet_干冰弹,
        # diancicichuan_电磁, bingbaofashengqi_冰爆, yueqiandianzi_电子,
      ]


class Game:
  back = (115, 2638, '返回',)


class Role(Game):
  """角色"""
  page = (334, 2630, '角色',)
  select_equipment_forging = (1026, 1136, '装备锻造',)

  class EquipmentForging:
    one_reinforcement = (861, 1958, '一键强化',)

  class Gemstone:
    one_click_synthesis = (1038, 1319, '一键合成',)


class Point:
  start_game = (630, 2312)
  jijia = (1111, 2023)
  exit_ = (884, 2343)
  random_skill = (621, 1810)


def is_game_over():
  return False


def have_play_advertisement():
  return False


def strong_equipment():
  """强化装备"""
  ADBHelper.touch(Role.page)
  ADBHelper.touch(Role.select_equipment_forging)
  ADBHelper.touch(Role.EquipmentForging.one_reinforcement)
  ADBHelper.touch(Role.back)


def synthetic_gem():
  """合成宝石"""
  ADBHelper.touch(Role.page)
  ADBHelper.touch(Role.Gemstone.one_click_synthesis)
  ADBHelper.touch(Role.page)


# todo 开始流程：点击开始，循环点击技能，1分30s释放机甲，结束返回

def play_game(play_num=1):
  device = ADBHelper.getDevicesList()[0]
  print(f'当前连接的设备是: {device}')

  # 刷新屏幕
  ADBHelper.screenCapture(device, Img.screen)

  while not ImageProc.locate(Img.screen, Img.Fight.back):

    point = ImageProc.locate(Img.screen, Img.Fight.start_game)
    # print(point)
    if point:
      print("开始游戏")
      ADBHelper.touch(device, point)
      time.sleep(REFRESH_TIME)
    ADBHelper.screenCapture(device, Img.screen)
    time.sleep(REFRESH_TIME)
    # for skill in Img.Fight.Skill.skill_first:
    #     point = ImageProc.locate(Img.screen, skill)
    #     if point:
    #         print("点击技能" + skill)
    #         # print(point)
    #         ADBHelper.touch(device, point)
    #         break
    point = ImageProc.locate(Img.screen, Img.Fight.Skill.gun)
    if point:
      print("点击枪技能")
      ADBHelper.touch(device, point)
    else:
      ADBHelper.touch(device, Point.random_skill)
  point = ImageProc.locate(Img.screen, Img.Fight.back)
  ADBHelper.touch(device, point)
  print("点击返回")
  time.sleep(REFRESH_TIME)


# 先通过坐标定位，然后通过图片定位，在通过文字定位

if __name__ == '__main__':
  for _ in range(2):
    play_game()

  # play_game()
  # point = ImageProc.locate(Img.screen, Img.Fight.start_game)
  # print(point)

  # point = ImageProc.find_text_position(Img.screen,"开始游戏")
  # print(point)

  # point = ImageProc.find_text_position(Img.screen,"角色")
  # print(point)
